<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 17/10/17
 * Time: 17:24
 */

Yaf_Loader::import(EXTEND_PATH.'/qcos/Cos.php');

class ModelModel
{
    /**
     * 图片上传
     */
    public function uploadAction($file)
    {
        $root = VERSION_LIVESTAR.'/Default/';
        $pathName['path'] = date('Y/m/');
        $pathName['name'] = dechex(microtime (true)*10000);
        $path = $root.$pathName['path'];
        $cos = new Cos();
        $type = pathinfo($file["name"]);
        $info = $cos->upload($file["tmp_name"],$path.$pathName['name'].'.'.$type['extension']);
        if($info){
            $data['path'] = $pathName['path'];
            $data['file'] = $pathName['name'].'.'.$type['extension'];
        }else{
            $data['code'] = 400;
            $data['error'] = '上传错误';
        }
        return $data;
    }

    public function moveImg($path,$file,$type)
    {
        $original = VERSION_LIVESTAR.'/Default/'.$path.$file;
        switch ($type){
            case 1:
                $address = VERSION_LIVESTAR.'/User/'.$path.$file;
                break;
            case 2:
                $address = VERSION_LIVESTAR.'/Society/'.$path.$file;
                break;
            case 3:
                $address = VERSION_LIVESTAR.'/Live/'.$path.$file;
                break;
            case 4:
                $address = VERSION_LIVESTAR.'/Group/'.$path.$file;
                break;
            case 5:
                $address = VERSION_LIVESTAR.'/Backdrop/'.$path.$file;
                break;
            case 6:
                $address = VERSION_LIVESTAR.'/Firm/'.$path.$file;
                break;
            default:
                $address = VERSION_LIVESTAR.'/Default/'.$path.$file;
                break;
        }
        $cos = new Cos();
        $cos->moveFile($original, '/'.$address);
        return HTTP_WEB.$address;
    }

    public function deleteImg($pathFile)
    {
        $cos = new Cos();
        $del = $cos->delFile($pathFile);
        if($del['code']){
            $msg['message'] = '图片删除错误:'.$del['message'];
        }else{
            $msg['message'] = '图片删除成功';
        }
        return $msg;
    }

}